// search_ticket.js
var api = require('../../utils/api');
var dateExtension = require('../../utils/date-extension');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    "searchQuery":{
      "departureCode":"",
      "arriveCode": "",
      "isChanges": 0,
      "travelDate": ""
    },
    "dateText":"",
    "ticket_list": []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.data.searchQuery.departureCode = options.departureCode;
    this.data.searchQuery.arriveCode = options.arriveCode;
    this.data.searchQuery.travelDate = options.travelDate;
    this.data.searchQuery.isChanges = options.isChanges;

    var dateText=  options.travelDate;
    this.setDateText(dateText);
    
    this.getSearchList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var dateValue = wx.getStorageSync('dateValue');
    this.data.searchQuery.travelDate = dateValue;
    this.setDateText(dateValue);
    this.getSearchList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.getSearchList();
    wx.hideNavigationBarLoading(); //完成停止加载
    wx.stopPullDownRefresh(); //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  getSearchList: function getSearchList() {
    var url = api.ticketRemainder();
    var params = this.data.searchQuery;
 
    var that = this;
    api.request(url, 'POST', params, function (res, err) {
      that.setData({ ticket_list: res });
    });
  },
  preSearchList: function (e) {
    
    var value = e.currentTarget.dataset.key;
    var date = this.data.searchQuery.travelDate;

    var travelDate = dateExtension.dayAdd(date, value);
    var url = api.ticketRemainder();
    this.data.searchQuery.travelDate = travelDate;
    this.setDateText(travelDate);
    this.getSearchList();
  },

  beforeSearchList: function (e) {
    
    var value = e.currentTarget.dataset.key;
    var date = this.data.searchQuery.travelDate;

    var travelDate = dateExtension.dayReducd(date, value);
    var url = api.ticketRemainder();
    this.data.searchQuery.travelDate = travelDate;
    this.setDateText(travelDate);
    this.getSearchList();
  },
  setDateText: function (dateValue) {
    var date = new Date(dateValue);
    var fDate = date.pattern("MM-dd EE");
    this.setData({
      "dateText": fDate
    });
  },
  changeDate:function(){

    var travelDate = this.data.searchQuery.travelDate;
    wx.navigateTo({
      url: '../date/date?selectDate=' + travelDate
    })
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }, handleTap1: function (e) {
    
    // var userInfo = app.globalData.userInfo;
    // if (!userInfo){
    //   app.openSetting(function(){
        var id = e.currentTarget.dataset.id;
        var departureCode = e.currentTarget.dataset.departurecode;
        var arriveCode = e.currentTarget.dataset.arrivecode;
        var params = "id=" + id + "&departureCode=" + departureCode + "&arriveCode=" + arriveCode;
        wx.navigateTo({
          url: '../orderinformation/order_infor?' + params
        })
      // });
    // }
  },
  setborder: function () {
    this.setData({
      setborder: '2rpx #fff solid',
      setborderRadius:'6rpx',
      settransform: 'rotate(180deg)',
      setbp: '-124rpx -2rpx',
      spendborder: '2rpx #00dad6 solid',
      spendborderRadius: '6rpx',
      spendbp: '-124rpx -8rpx',
      spendtransform: 'rotate(0deg)',
      arriveborder: '2rpx #00dad6 solid',
      arriveborderRadius: '6rpx',
      arrivetransform: 'rotate(0deg)',
      arrivebp: '-124rpx -8rpx',
    });
},
  spendborder: function () {
    this.setData({
      setborder: '2rpx #00dad6 solid',
      setborderRadius: '6rpx',
      settransform: 'rotate(0deg)',
      setbp:'-124rpx -8rpx',
      spendborder: '2rpx #fff solid',
      spendborderRadius: '6rpx',
      spendbp: '-124rpx -2rpx',
      spendtransform: 'rotate(180deg)',
      arriveborder: '2rpx #00dad6 solid',
      arriveborderRadius: '6rpx',
      arrivetransform: 'rotate(0deg)',
      arrivebp: '-124rpx -8rpx',
    });
 },
  arriveborder: function () {
    this.setData({
      setborder: '2rpx #00dad6 solid',
      setborderRadius:'6rpx',
      settransform: 'rotate(0deg)',
      setbp: '-124rpx -8rpx',
      spendborder: '2rpx #00dad6 solid',
      spendborderRadius: '6rpx',
      spendtransform: 'rotate(0deg)',
      spendbp: '-124rpx -8rpx',
      arriveborder: '2rpx #fff solid',
      arriveborderRadius: '6rpx',
      arrivetransform: 'rotate(180deg)',
      arrivebp: '-124rpx -2rpx',
    });
  },
})